<?php


namespace Xiaoguai\DesignPattern\DP022;


class Structure
{
    /**
     * @var ElementInterface[] $elements
     */
    protected $elements = [];

    public function addElement(ElementInterface $element)
    {
        $this->elements[] = $element;
    }

    public function removeElement(ElementInterface $element)
    {
        $index = array_search($element, $this->elements, true);
        if ($index !== false) {
            array_splice($this->elements, $index, 1);
        }
    }

    /**
     * 透传给elements的accept,将element自己交给visitor访问(visit)
     * @param VisitorInterface $visitor
     */
    public function accept(VisitorInterface $visitor)
    {
        foreach ($this->elements as $element) {
            $element->accept($visitor);
        }
    }
}